package com.ugoodtech.mdcc.client.controller;

import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.service.UserProfilesService;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.repository.UserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@Api(description = "用户详情相关接口")
@RestController
@RequestMapping("/userprofiles")
public class UserProfilesController {

    @Autowired
    private UserProfilesService userProfilesService;

    @Autowired
    private UserRepository userRepository;

    @ApiOperation("关键字查询用户名字")
    @RequestMapping(value = "getUserName", method = RequestMethod.GET)
    public JsonResponse getUserName(Principal principal,
                                    @ApiParam("用户名字关键字") @RequestParam(value = "userName", required = false) String userName) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
        return userProfilesService.findByName(userName);
    }

    @ApiOperation("查询投诉报告指派的用户名字")
    @RequestMapping(value = "getUserName_Province_Brand", method = RequestMethod.GET)
    public JsonResponse getUserName_Province_Brand(Principal principal,
                                                   @ApiParam("用户名字关键字") @RequestParam(value = "userName", required = false) String userName,
                                                   @ApiParam("省份") @RequestParam(value = "province", required = false) String province,
                                                   @ApiParam("品牌") @RequestParam(value = "brandName", required = false) String brandName) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
        return userProfilesService.getUserName(userName, province, brandName);
    }

    @ApiOperation("检查被选中的人是否有权限")
    @RequestMapping(value = "checkUser_Province_Brand", method = RequestMethod.GET)
    public JsonResponse checkUser_Province_Brand(Principal principal,
                                                 @ApiParam("用户名字关键字") @RequestParam(value = "userId") Long userId,
                                                 @ApiParam("省份") @RequestParam(value = "province", required = false) String province,
                                                 @ApiParam("品牌") @RequestParam(value = "brandName", required = false) String brandName,
                                                 @ApiParam("投诉报告ID") @RequestParam(value = "complaintId", required = false) Long solutionId) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
        return userProfilesService.checkUser(userId, province, brandName, solutionId);
    }

}
